<template>
  <div class="hzy_employee">
    <el-button type="primary" @click="addClick">增加员工</el-button>
    <el-button type="warning" @click="setGroup">设置分组</el-button>
    <el-button :type="a" @click="BulkDeletion">批量删除</el-button>
    <el-input
      v-model="siftCondition"
      placeholder="请输入筛选条件"
      autofocus="true"
    ></el-input>
    <el-button type="primary" @click="search">查询</el-button>
    <!-- 员工表格 -->
    <el-table
      ref="multipleTable"
      :data="employeeData"
      tooltip-effect="dark"
      style="width: 100%; margin-top: 20px"
      @selection-change="handleSelectionChange"
      border
    >
      <el-table-column
        type="selection"
        width="55"
        align="center"
      ></el-table-column>
      <el-table-column prop="id" label="序号" width="60" align="center">
      </el-table-column>
      <el-table-column
        prop="name"
        label="姓名"
        width="100"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="fenDianName"
        label="所属门店"
        width="100"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="nameId"
        label="员工编码"
        width="100"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="zuMing"
        label="组名"
        width="100"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="userName"
        label="用户名"
        width="120"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="jiaoSe"
        label="角色"
        width="100"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="idCard"
        label="身份证号"
        width="165"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="phone"
        label="联系电话"
        width="120"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="zhuangTai"
        label="当前状态"
        width="100"
        align="center"
      >
        {{ isWork }}
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="160" align="center">
        <template slot-scope="scope">
          <el-button
            @click="editClick(scope.$index, scope.row)"
            type="success"
            size="mini"
            >编辑</el-button
          >
          <el-button
            @click="delEmployeeClick(scope.row.id)"
            type="danger"
            size="mini"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page.sync="page"
      :page-sizes="[5, 10, 15, 20, 25, 30]"
      :page-size="5"
      layout="sizes, prev, pager, next"
      :total="totalCountNum"
    >
    </el-pagination>
    <AddEmployee v-if="isShowAdd" :isShowAdd="isShowAdd" @closeAdd="closeAdd" />
    <SetEmployeeGroup
      v-if="isShowSet"
      :isShowSet="isShowSet"
      @closeSet="closeSet"
    />
    <EditEmployee
      v-if="isShowEdit"
      :isShowEdit="isShowEdit"
      @closeEdit="closeEdit"
      :editList="editList"
    />
  </div>
</template>

<script>
import AddEmployee from "@/components/EmployeeSet/AddEmployee.vue";
import SetEmployeeGroup from "@/components/EmployeeSet/SetEmployeeGroup.vue";
import EditEmployee from "@/components/EmployeeSet/EditEmployee.vue";
import {
  getEmployeeListApi,
  delEmployeeApi,
  delallApi,
  searchEmployeeApi,
} from "@/http/employeeListApi";
export default {
  name: "EmployeeInfo",
  components: {
    AddEmployee,
    SetEmployeeGroup,
    EditEmployee,
  },
  data() {
    return {
      employeeData: [],
      isShowAdd: false, // 新增员工界面
      isShowSet: false, // 设置分组界面
      isShowEdit: false, // 编辑员工界面
      editList: {},
      addList: {},
      a: "", // 批量删除按钮状态
      page: 1, // 当前页
      size: 5, //当前页面展示的数量
      totalCountNum: 25, // 员工总数量
      isWork: "", // 员工状态
      multipleSelection: "", // 多重选择删除
      siftCondition: "", // 筛选输入数值
    };
  },
  created() {
    this.getInit(this.page, this.size);
  },

  methods: {
    handleSizeChange(size) {
      // 每页展示的数量
      console.log("每页" + size + "条");
      this.size = size;
      this.getInit(this.page, size);
    },
    handleCurrentChange(page) {
      // 当前分页
      console.log("当前页:", page);
      this.page = page;
      this.getInit(page, this.size);
    },
    handleSelectionChange(val) {
      // 全选 批量删除
      this.multipleSelection = val;
      console.log(val);
      if (val.length >= 2) {
        this.a = "danger";
      } else {
        this.a = "";
      }
    },
    getInit(page, size) {
      // getEmployeeTotal()
      //   .then((res) => {
      //     this.totalCountNum = res.data.data;
      //     console.log(res.data.data);
      //   })
      //   .catch((err) => {
      //     console.log("获取总数量失败：", err);
      //   });
      getEmployeeListApi(page, size)
        .then((res) => {
          this.employeeData = res.data.data;
          console.log(res.data.data.length);
          for (var i = 0; i < res.data.data.length; i++) {
            if (res.data.data[i].zhuangTai === false) {
              this.isWork = "离职";
            } else if (res.data.data[i].zhuangTai === true) {
              this.isWork = "在职";
            }
          }
        })
        .catch((err) => {
          console.log("获取列表失败：", err);
        });
    },
    search() {
      // 模糊查询
      console.log(this.siftCondition);
      if (this.siftCondition == "") {
        return this.$message({
          type: "warning",
          message: "请输入条件",
        });
      }
      searchEmployeeApi(this.siftCondition)
        .then((res) => {
          this.siftCondition = ""; // 制空
          if (res.data.code !== 200) {
            return this.$message({
              type: "error",
              message: "查询失败",
            });
          }
          this.getInit(this.page, this.size); //重新渲染
          console.log("查询成功：", res);
          this.$message({
            type: "success",
            message: "查询成功",
          });
        })
        .catch((err) => {
          console.log("查询失败：", err);
        });
    },
    addClick() {
      // 新增员工界面显示
      this.isShowAdd = true;
    },
    closeAdd() {
      // 关闭新增员工界面
      this.isShowAdd = false;
    },
    setGroup() {
      // 设置分组界面显示
      this.isShowSet = true;
    },
    closeSet() {
      // 关闭设置分组界面
      this.isShowSet = false;
    },
    editClick($index, row) {
      // 编辑员工界面显示
      this.isShowEdit = true;
      console.log($index, row);
      this.editList = row;
    },
    closeEdit() {
      // 关闭编辑员工界面
      this.isShowEdit = false;
    },
    delEmployeeClick(id) {
      // 删除员工信息
      console.log(id);
      this.$confirm("此操作将永久删除该数据, 是否继续?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "error",
      })
        .then(() => {
          // 删除员工返回的 id
          delEmployeeApi(id)
            .then((res) => {
              console.log(id);
              console.log("删除成功：", res);
              this.getInit(this.page, this.size);
            })
            .catch((err) => {
              console.log("删除失败：", err);
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    BulkDeletion(ids) {
      // 批量删除
      console.log(this.multipleSelection);
      ids = this.multipleSelection
        .map(function (elem) {
          return elem.id;
        })
        .join(",");
      console.log(ids);
      this.$confirm("此操作将永久删除所选数据, 是否继续?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "error",
      })
        .then(() => {
          delallApi(ids)
            .then((res) => {
              console.log("删除成功：", res);
              this.getInit(this.page, this.size);
            })
            .catch((err) => {
              console.log("删除失败：", err);
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>

<style lang="scss" scoped>
.el-input {
  width: 200px;
  margin-left: 10px;
  margin-right: 10px;
}
</style>